{
  This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
  the Free Software Foundation; version 2 of the License.
   
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
}

// Copyright (c) 2010 2011 2012 - J. Aldo G. de Freitas Junior

{$mode objfpc}
{$H+}

Unit
	BaseException;

Interface

Uses
	Classes,
	SysUtils;

Type
	EInterpreterException = Class(Exception)
	Private
		fStringList : TStringList;
	Public
		Constructor Create(Const aRow, aCol : Integer; Const aModule : String; Const aPrevious : Exception);
		Procedure AddTrace(Const aRow, aCol : Integer; Const aModule, aMessage : String);
		Destructor Destroy; Override;
		Function AsText : String;
		Property Messages  : TStringList Read fStringList;
	End;

Implementation

Constructor EInterpreterException.Create(Const aRow, aCol : Integer; Const aModule : String; Const aPrevious : Exception);
Begin
	Inherited Create('');
	fStringList := TStringList.Create;
	fStringList.Duplicates := dupIgnore;
	fStringList.Sorted := False;
	fStringList.Add(aModule + '(' + IntToStr(aRow) + ',' + IntToStr(aCol) + ')');
	If Assigned(aPrevious) Then
		If aPrevious Is EInterpreterException Then
		fStringList.AddStrings((aPrevious As EInterpreterException).Messages)
	Else
		fStringList.Add('Error : ' + aPrevious.Message);
End;

Procedure EInterpreterException.AddTrace(Const aRow, aCol : Integer; Const aModule, aMessage : String);
Begin
	fStringList.Add(aModule + '(' + IntToStr(aRow) + ',' + IntToStr(aCol) + '): ' + aMessage);
End;

Destructor EInterpreterException.Destroy;
Begin
	FreeAndNil(fStringList);
	Inherited Destroy;
End;

Function EInterpreterException.AsText : String;
Var
	lCtrl : Integer;
Begin
	For lCtrl := 0 To fStringList.Count - 1 Do
		fStringList[lCtrl] := #09 + fStringList[lCtrl];
	Result := 'Call trace: ' + LineEnding + fStringList.Text
End;

End.
